#include "mtrack.h"

MTrack::MTrack(QObject *parent) :
    QAbstractTableModel(parent)
{
}

MTrack::~MTrack()
{
}

Qt::ItemFlags MTrack::flags(const QModelIndex& index) const
{
    Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
    if (index.isValid())
    {
        if(index.column() == DtTrack::Name)
            theFlags |= Qt::ItemIsSelectable;
    }
    return theFlags;
}

QVariant MTrack::data(const QModelIndex& index, int role) const
{
  if (!index.isValid() ||
      index.row() < 0 || index.row() >= mTrack.size() ||
      index.column() < 0 || index.column() >= DtTrack::NbAttr)
    return QVariant();
  DtTrack *track = mTrack[index.row()];
  if(role == Qt::DisplayRole || role == Qt::EditRole)
  {
      switch(index.column())
      {
      case DtTrack::Play:
          return track->getPlay();
      case DtTrack::Add:
          return track->getAdd();
      case DtTrack::Number:
          return track->getNumber() == 0 ? ""
                                         : QString::number(track->getNumber());
      case DtTrack::Name:
          return track->getName();
      case DtTrack::Time:
          return track->getTime();
      case DtTrack::Score:
          return track->getScore();
      case DtTrack::Heart:
          return track->getHeartDelegate();
      case DtTrack::Artist:
          return (track->getArtists().first() == NULL ? "" :
                                                track->getArtists().first()
                                                ->getName());
      case DtTrack::Genre:
          return (track->getGenres().first() == NULL ? "" :
                                               track->getGenres().first()
                                               ->getName());
      case DtTrack::Album:
          return (track->getAlbums().first() == NULL ? "" :
                                              track->getAlbums().first()
                                              ->getName());
      default: ;
      }
  }
  if(role == Qt::ToolTipRole)
      return track->getName();

  return QVariant();
}

QVariant MTrack::headerData(int section, Qt::Orientation orientation,
                             int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    if (orientation == Qt::Horizontal && section < DtTrack::NbAttr)
    {
        if(section == DtTrack::Name)
            return QObject::tr("Name");
        else if(section == DtTrack::Number)
            return QObject::tr("#");
        else if(section == DtTrack::Score)
            return QObject::tr("Score");
        else if(section == DtTrack::Time)
            return QObject::tr("Time");
        else if(section == DtTrack::Artist)
            return QObject::tr("Artist");
        else if(section == DtTrack::Album)
            return QObject::tr("Album");
        else if(section == DtTrack::Genre)
            return QObject::tr("Genre");
    }
    return "";
}

int MTrack::rowCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : mTrack.size();
}

int MTrack::columnCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : DtTrack::NbAttr;
}

bool MTrack::insertRows(int , int , const QModelIndex&)
{
    return false;
}

bool MTrack::removeRows(int row, int count, const QModelIndex&)
{
  beginRemoveRows(QModelIndex(), row, row + count - 1);
  for (int i = 0; i < count; ++i)
    mTrack.removeAt(row);
  endRemoveRows();
  return true;
}

QList<QHeaderView::ResizeMode> MTrack::getHorizontalResizeModes() const
{
    QList<QHeaderView::ResizeMode> t;
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Stretch);
    t.append(QHeaderView::Stretch);
    t.append(QHeaderView::Stretch);
    t.append(QHeaderView::Fixed);
    return t;
}

void MTrack::setList(QList<DtTrack *> &lTrack)
{
    mTrack = lTrack;
    reset();
}

QList<DtTrack *> MTrack::getList() const
{
    return mTrack;
}
